package com.jihox.abookpro.util;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBManager {
	private DBHelper helper;
	private SQLiteDatabase db;

	public DBManager(Context context) {
		helper = new DBHelper(context);
		//因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
		//所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
		db = helper.getWritableDatabase();
	}

	/**
	 * add androidImageFiles
	 * @param androidImageFiles
	 */
	public void addThumbnails(List<AndroidImageFile> androidImageFiles) {
		db.beginTransaction();	//开始事务
		try {
			for (AndroidImageFile androidImageFile : androidImageFiles) {
				//        		Log.i("idid",androidImageFile.getPath());
				//        		db.execSQL("INSERT INTO OR REPLACE INTO thumbnails VALUES(?, ?, ?,?)", new Object[]{androidImageFile.getPath(), androidImageFile.getModifiedDate(), androidImageFile.getFolderName(),androidImageFile.getOrientation()});
				ContentValues cv = new ContentValues();
				cv.put("_data", androidImageFile.getPath());
				cv.put("date_modified", androidImageFile.getModifiedDate());
				cv.put("orientation", androidImageFile.getOrientation());
				cv.put("bucket_display_name", androidImageFile.getFolderName());

				db.insertWithOnConflict("thumbnails", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
			}
			db.setTransactionSuccessful();	//设置事务成功完成
		} finally {
			db.endTransaction();	//结束事务
		}
	}

	/**
	 * update androidImageFile's age
	 * @param androidImageFile
	 */
	public void updateThumbnails(AndroidImageFile androidImageFile) {
		ContentValues cv = new ContentValues();
		cv.put("orientation", androidImageFile.getOrientation());
		cv.put("date_modified", androidImageFile.getModifiedDate());
		db.update("thumbnails", cv, "_data = ?", new String[]{androidImageFile.getPath()});
	}

	/**
	 * delete old androidImageFile
	 * @param androidImageFile
	 */
	public void deleteOldThumbnails(AndroidImageFile androidImageFile) {
		db.delete("thumbnails", "_data = ?", new String[]{String.valueOf(androidImageFile.getPath())});
	}

	/**
	 * query all androidImageFiles, return list
	 * @return List<AndroidImageFile>
	 */
	public List<AndroidImageFile> queryAllThumbnails() {
		ArrayList<AndroidImageFile> androidImageFiles = new ArrayList<AndroidImageFile>();
		Cursor c = queryTheCursor();
		while (c.moveToNext()) {
			AndroidImageFile androidImageFile = new AndroidImageFile();
			androidImageFile.setPath( c.getString(c.getColumnIndex("_data")));
			androidImageFile.setModifiedDate( c.getInt(c.getColumnIndex("date_modified")));
			androidImageFile.setFolderName( c.getString(c.getColumnIndex("bucket_display_name")));
			androidImageFile.setOrientation(c.getInt(c.getColumnIndex("orientation")));
			androidImageFiles.add(androidImageFile);
		}
		c.close();
		return androidImageFiles;
	}

	/**
	 * query all androidImageFiles, return cursor
	 * @return	Cursor
	 */
	public Cursor queryTheCursor() {
		Cursor c = db.rawQuery("SELECT * FROM thumbnails ORDER BY date_modified DESC", null);
		return c;
	}

	/**
	 * close database
	 */
	public void closeDB() {
		db.close();
	}
}

